const express=require('express')
const path=require('path')
let app=express()
app.listen(3000,()=>{
    console.log('3000ok');
})


app.use( express.urlencoded({extended:true}) )
app.use( express.json())



const query=require("./tools/db")


app.get("/students",async (req,res)=>{


    console.log(  req.query ,"get参数"  );


    let [err,result]=await query(`select *  from  xsb`)


    if(err){
        res.send({
            status:500,
            msg:"服务器出错了"
        })
        return
    }

    res.send({
        status:200,
        msg:"请求成功",
        result
    })

})


app.post("/students",async (req,res)=>{

    console.log(req.body,"post参数");


    let [err,result]=await query(`select *  from  xsb`)

    if(err){
        res.send({
            status:500,
            msg:"服务器出错了"
        })
        return
    }

    res.send({
        status:200,
        msg:"post请求成功",
        result
    })

})


// 目前页面和数据必须在同一个服务器上，所以必须使用渲染页面的形式去打开
app.get("/index",(req,res)=>{
    res.sendFile( path.join(__dirname,"./views/index.html") )
})